#!/bin/sh

binary="/sbin/dhclient"
pidfile="/var/run/dhclient-${interface}.pid"

[ -e /etc/conf.d/dhclient ] && source /etc/conf.d/dhclient
[ ! -d /var/lib/dhcp ] && mkdir -p /var/lib/dhcp

dhclient_defaults="-lf /var/lib/dhcp/dhclient.${interface}.leases -df /var/lib/dhcp/dhclient6.${interface}.leases"

netif_pre_up() {
    if [ ! -f "${binary}" ]; then
        echo "dhclient binary not found. Did you emerge net-misc/dhcp?"
        exit 1
    fi
}

netif_post_up() {
    if [ -f "${pidfile}" ]; then
        echo "There's a PID file for interface $interface. Aborting."
        exit 2
    fi

    $dhclient $dhclient_defaults -pf $pidfile $dhclient_defaults $interface
}

netif_pre_down() {
    if [ ! -f "${pidfile}" ]; then
        echo "There's no PID file for interface ${interface}. Aborting."
        exit 3
    fi
    start-stop-daemon --stop --exec "${binary}" --pidfile "${pidfile}"
}
